package com.miracle9.game.util;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class scoreResultProcess
{
  public Random aw;
  public static final float ax = 2.50663F;
  public ArrayList<Integer> ay = new ArrayList();
  private int az;
  private int aA;
  
  public scoreResultProcess()
  {
    this.aw = new Random(System.currentTimeMillis());
  }
  
  public void addInteger(int sumValue, int num)
  {
    int count = 0;
    int sum = 0;
    int max = 0;
    thiTimeResult(sumValue, num);
    for (int i = 0; i < num; i++)
    {
      int tmp = processErrorScore();
      if (tmp > 0)
      {
        count++;
        sum += tmp;
        if (tmp > max) {
          max = tmp;
        }
      }
      GameParameterAssist.aG.add(Integer.valueOf(tmp));
    }
  }
  
  public void thiTimeResult(int sumValue, int num)
  {
    this.ay.clear();
    if ((sumValue <= 0) || (num <= 0)) {
      return;
    }
    this.az = sumValue;
    this.aA = num;
    
    int RealBonusNum = 0;
    int nLeftSumValue = sumValue;
    double dExpect = 0.0D;
    double dMax = 0.0D;
    if (sumValue > num)
    {
      RealBonusNum = (int)(Math.ceil(num * 0.8D) + 0.1D);
      dExpect = sumValue / RealBonusNum;
      dMax = 4.0D * dExpect;
      this.az = ((int)Math.ceil(dMax + dExpect));
    }
    else
    {
      RealBonusNum = (int)(Math.ceil(sumValue * 0.5D) + 0.1D);
      dExpect = sumValue / RealBonusNum;
      dMax = 2.0D * dExpect;
      this.az = ((int)Math.ceil(dMax + dExpect));
    }
    for (int i = 0; i < num; i++)
    {
      int nTmpValue = (int)(incomeResult(dExpect, dExpect, 0.0D, dMax) + 0.5D);
      if (nTmpValue < nLeftSumValue)
      {
        this.ay.add(Integer.valueOf(nTmpValue));
        nLeftSumValue -= nTmpValue;
      }
      else if (nLeftSumValue > 0)
      {
        this.ay.add(Integer.valueOf(nLeftSumValue));
        nLeftSumValue = 0;
      }
      else
      {
        this.ay.add(Integer.valueOf(0));
        nLeftSumValue = 0;
      }
    }
    if (nLeftSumValue > 0)
    {
      int nAver = nLeftSumValue / num;
      int nTheLast = nLeftSumValue - (num - 1) * nAver;
      for (int i = 0; i < num; i++) {
        if (i == num - 1) {
          this.ay.set(i, Integer.valueOf(((Integer)this.ay.get(i)).intValue() + nTheLast));
        } else {
          this.ay.set(i, Integer.valueOf(((Integer)this.ay.get(i)).intValue() + nAver));
        }
      }
    }
  }
  
  public int processErrorScore()
  {
    if (this.ay.isEmpty()) {
      return 0;
    }
    int value = 0;
    int tmpNum = this.ay.size();
    int tmpRand = this.aw.nextInt(tmpNum);
    value = ((Integer)this.ay.get(tmpRand)).intValue();
    this.ay.remove(tmpRand);
    this.aA -= 1;
    if ((value > this.az) || (this.aA < 0))
    {
      value = 0;
      System.out.println("ther occurs a error!");
    }
    return value;
  }
  
  private double score(double min, double max)
  {
    double dRand = this.aw.nextDouble();
    return min + (max - min) * dRand;
  }
  
  private double MathExp(double x, double miu, double sigma)
  {
    return 1.0D / (2.506629943847656D * sigma) *Math.exp(-1.0D * (x - miu) * (x - miu) / (2.0D * sigma * sigma));
  }
  
  private double incomeResult(double miu, double sigma, double min, double max)
  {
    double x;
    double y;
    double dScope;
    do
    {
      x = score(min, max);
      y = MathExp(x, miu, sigma);
      dScope = score(0.0D, MathExp(miu, miu, sigma));
    } 
    while (dScope > y);
    return x;
  }
}
